﻿namespace ProblemsSet
{
    public class Problem_127 : BaseProblem
    {
        public override object GetResult()
        {
            const long max = 120000;

            long res = 0;

            for (long a = 1; a <= max/2; a++)
            {
                for (long b = a+1; b <= max-1; b++)
                {
                    long c = a + b;
                    if (c >= max)
                        break;
                    if (MathLogic.GetNOD(a, b) != 1) continue;
                    if (MathLogic.GetNOD(a, c) != 1) continue;
                    if (MathLogic.GetNOD(b, c) != 1) continue;
                    var tmpC = MathLogic.GetPrimeFactors(c);
                    var ex = false;
                    long rs = 1;

                    bool exx = false;
                    foreach (var pair in tmpC)
                    {
                        rs *= pair.Key;
                        if (pair.Value > 1)
                        {
                            exx = true;
                        }
                        if (rs >= c)
                        {
                            ex = true;
                            break;
                        }
                    }
                    if (!exx || ex)
                        continue;
                    var tmpB = MathLogic.GetPrimeFactors(b);
                    foreach (var pair in tmpB)
                    {
                        rs *= pair.Key;
                        if (rs >= c)
                        {
                            ex = true;
                            break;
                        }
                    }
                    if (ex)
                        continue;
                    var tmpA = MathLogic.GetPrimeFactors(a);
                    foreach (var pair in tmpA)
                    {
                        rs *= pair.Key;
                        if (rs >= c)
                        {
                            ex = true;
                            break;
                        }
                    }
                    if (ex)
                        continue;

                    res += c;
                }
            }

            return res;
        }

        public override string Problem
        {
            get
            {
                return @"The radical of n, rad(n), is the product of distinct prime factors of n. For example, 504 = 23  32  7, so rad(504) = 2  3  7 = 42.

We shall define the triplet of positive integers (a, b, c) to be an abc-hit if:

GCD(a, b) = GCD(a, c) = GCD(b, c) = 1
a  b
a + b = c
rad(abc)  c
For example, (5, 27, 32) is an abc-hit, because:

GCD(5, 27) = GCD(5, 32) = GCD(27, 32) = 1
5  27
5 + 27 = 32
rad(4320) = 30  32
It turns out that abc-hits are quite rare and there are only thirty-one abc-hits for c  1000, with c = 12523.

Find c for c  120000.

Note: This problem has been changed recently, please check that you are using the right parameters.";
            }
        }

        public override bool IsSolved
        {
            get
            {
                return true;
            }
        }

        public override object Answer
        {
            get
            {
                return 18407904;
            }
        }
    }
}
